# This makefile is made by Mads Pedersen and Kristian Theilgaard Hansen for our university project at UCSD.
# No copyrights what so ever, most was found on stackoverflow.
NAME	= GrphEngn
ECHO	= @echo
CC	= @g++
LIBS	= -lglut -lGLU -lGL -lXmu -lXi -lGLee 
LDFLAGS	= $(LIBS)
CFLAGS	= 
DIRS = . Culling LinAlg SceneGraph SceneGraph/Primitives SceneGraph/NonPrimitives Shaders Utils
SRC_DIR   := $(addprefix src/,$(DIRS))
BUILD_DIR := $(addprefix build/,$(DIRS))
SRC       := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))
OBJ       := $(patsubst src/%.cpp,build/%.o,$(SRC))
INCLUDES  := $(addprefix -I,$(SRC_DIR))

vpath %.cpp $(SRC_DIR)

define make-goal
$1/%.o: %.cpp
	$(ECHO) "Compiling $$<"
	$(CC) $(CFLAGS) $(INCLUDES) -c $$< -o $$@ 
endef

.PHONY: all checkdirs clean

all: checkdirs build/$(NAME)

build/$(NAME): $(OBJ)
	$(ECHO) "Linking $@..."
	$(CC) $^ -o $@ $(LIBS)

checkdirs: $(BUILD_DIR)

$(BUILD_DIR):
	@mkdir -p $@

clean:
	@rm -rf build/*
	$(ECHO) "All clean!"

new:
	make clean
	make 

$(foreach bdir,$(BUILD_DIR),$(eval $(call make-goal,$(bdir))))
